﻿using Teigha.Core;
using Teigha.TD;

namespace OdaOperation.Core.Creators
{
    public class LineCreator : BaseCreator
    {
        /// <summary>
        /// 构造函数，接受数据库对象作为参数
        /// </summary>
        /// <param name="pDb"></param>
        public LineCreator(OdDbDatabase pDb)
            : base(pDb)
        {
        }

        /// <summary>
        /// 构造函数，接受数据库对象和块表记录ID作为参数
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="blockTableId"></param>
        public LineCreator(OdDbDatabase pDb, OdDbObjectId blockTableId)
            : base(pDb, blockTableId)
        {
        }

        /// <summary>
        /// 构造函数，接受数据库对象和图层名称作为参数
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="layerName"></param>
        public LineCreator(OdDbDatabase pDb, string layerName)
            : base(pDb, layerName)
        {
        }

        /// <summary>
        /// 构造函数，接受数据库对象、块表记录ID和图层名称作为参数
        /// </summary>
        /// <param name="pDb"></param>
        /// <param name="blockTableId"></param>
        /// <param name="layerName"></param>
        public LineCreator(OdDbDatabase pDb, OdDbObjectId blockTableId, string layerName)
            : base(pDb, blockTableId, layerName)
        {
        }

        /// <summary>
        /// 创建线条的方法，接受块表记录ID、图层名称、起点和终点作为参数
        /// </summary>
        /// <param name="blockTableId"></param>
        /// <param name="layerName"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public OdDbLine CreateLine(OdDbObjectId blockTableId, string layerName, OdGePoint3d start, OdGePoint3d end)
        {
            OdDbBlockTableRecord blockTableRecord = blockTableId.openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            OdDbLine line = OdDbLine.createObject();
            line.setStartPoint(start);
            line.setEndPoint(end);
            line.setDatabaseDefaults(m_database);
            line.setLayer(layerName);
            blockTableRecord.appendOdDbEntity(line);
            return line;
        }

        /// <summary>
        /// 创建线条的方法，接受图层名称、起点和终点作为参数
        /// </summary>
        /// <param name="layerName"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public OdDbLine CreateLine(string layerName, OdGePoint3d start, OdGePoint3d end)
        {
            OdDbBlockTableRecord blockTableRecord = m_blockTableId.openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            OdDbLine line = OdDbLine.createObject();
            line.setStartPoint(start);
            line.setEndPoint(end);
            line.setDatabaseDefaults(m_database);
            line.setLayer(layerName);
            blockTableRecord.appendOdDbEntity(line);
            return line;
        }

        /// <summary>
        /// 创建线条的方法，接受块表记录ID、起点和终点作为参数
        /// </summary>
        /// <param name="blockTableId"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public OdDbLine CreateLine(OdDbObjectId blockTableId, OdGePoint3d start, OdGePoint3d end)
        {
            OdDbBlockTableRecord blockTableRecord = blockTableId.openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            OdDbLine line = OdDbLine.createObject();
            line.setStartPoint(start);
            line.setEndPoint(end);
            line.setDatabaseDefaults(m_database);
            line.setLayer(m_layerName);
            blockTableRecord.appendOdDbEntity(line);
            return line;
        }

        /// <summary>
        /// 创建线条的方法，接受起点和终点作为参数
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <returns></returns>
        public OdDbLine CreateLine(OdGePoint3d start, OdGePoint3d end)
        {
            // 如果块表记录ID为空，则使用模型空间ID
            OdDbBlockTableRecord blockTableRecord = (m_blockTableId ?? m_database.getModelSpaceId()).openObject(OpenMode.kForWrite) as OdDbBlockTableRecord;
            OdDbLine line = OdDbLine.createObject();
            line.setStartPoint(start);
            line.setEndPoint(end);
            line.setDatabaseDefaults(m_database);
            line.setLayer(m_layerName);
            blockTableRecord.appendOdDbEntity(line);
            return line;
        }
    }

}